package com.topisv.tms.utils.lead;


import java.util.Date;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.ss.usermodel.Cell;

import com.topisv.tms.utils.DateUtils;

/**
 * Excel工具类
 * 
 * @author chenhogntao
 *
 */
public class ExcelUtil {
	
	/**
	 * 单元格取值转换
	 * 
	 * @param cell
	 * @return
	 */
	public static String getCellStringValue(Cell cell) {
        String cellValue = "";      
        switch (cell.getCellType()) {
            //字符串类型  
        case HSSFCell.CELL_TYPE_STRING:
            cellValue = cell.getStringCellValue();
            if(cellValue.trim().equals("")||cellValue.trim().length()<=0) {
                cellValue=" ";
            }
            break;
            // 数字
        case HSSFCell.CELL_TYPE_NUMERIC:
        	if (HSSFDateUtil.isCellDateFormatted(cell)) {
        		Date date = cell.getDateCellValue();
        		cellValue = DateUtils.formatDate(date);
        		
        	} else {
        		cellValue = String.valueOf(cell.getNumericCellValue());
        	}
            break;
            // 表达式类型
        case HSSFCell.CELL_TYPE_FORMULA: 
            cell.setCellType(HSSFCell.CELL_TYPE_NUMERIC);      
            cellValue = String.valueOf(cell.getNumericCellValue());      
            break;
            // 为空时
        case HSSFCell.CELL_TYPE_BLANK:
            cellValue=" ";
            break;
            // 布尔类型
        case HSSFCell.CELL_TYPE_BOOLEAN:
            break;
            // 异常类型
        case HSSFCell.CELL_TYPE_ERROR:
            break;
        default:
        	cellValue = String.valueOf(cell.getStringCellValue());
            break;
        }
        return cellValue;      
    }

}
